package com.ruyuan.eshop.order.service.impl;

import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.enums.OrderStatusEnum;
import com.ruyuan.eshop.common.utils.ParamCheckUtil;
import com.ruyuan.eshop.order.dao.*;
import com.ruyuan.eshop.order.domain.dto.CheckLackDTO;
import com.ruyuan.eshop.order.domain.dto.LackDTO;
import com.ruyuan.eshop.order.domain.dto.LackItemDTO;
import com.ruyuan.eshop.order.domain.entity.OrderInfoDO;
import com.ruyuan.eshop.order.domain.entity.OrderItemDO;
import com.ruyuan.eshop.order.domain.request.LackItemRequest;
import com.ruyuan.eshop.order.domain.request.LackRequest;
import com.ruyuan.eshop.order.exception.OrderBizException;
import com.ruyuan.eshop.order.exception.OrderErrorCodeEnum;
import com.ruyuan.eshop.order.manager.OrderNoManager;
import com.ruyuan.eshop.order.service.AfterSaleAmountService;
import com.ruyuan.eshop.order.service.OrderLackService;
import com.ruyuan.eshop.product.api.ProductApi;
import com.ruyuan.eshop.product.domain.dto.ProductSkuDTO;
import com.ruyuan.eshop.product.domain.query.ProductSkuQuery;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxg
 * @Description: OrderLackServiceImpl
 * @date 2022/4/1615:37
 */
@Service
public class OrderLackServiceImpl implements OrderLackService {

    @Autowired
    private OrderInfoDAO orderInfoDAO;

    @Autowired
    private OrderItemDAO orderItemDAO;

    @Autowired
    private AfterSaleInfoDAO afterSaleInfoDAO;

    @Autowired
    private AfterSaleItemDAO afterSaleItemDAO;

    @Autowired
    private AfterSaleRefundDAO afterSaleRefundDAO;

    @Autowired
    private OrderNoManager orderNoManager;

    @Autowired
    private AfterSaleAmountService afterSaleAmountService;

    @DubboReference(version = "1.0.0")
    private ProductApi productApi;

    @Override
    public CheckLackDTO checkRequest(LackRequest request) throws OrderBizException {
        //1、参数基本校验
        ParamCheckUtil.checkStringNonEmpty(request.getOrderId(), OrderErrorCodeEnum.ORDER_ID_IS_NULL);
        ParamCheckUtil.checkCollectionNonEmpty(request.getLackItems(), OrderErrorCodeEnum.LACK_ITEM_IS_NULL);
        //2、查询订单
        OrderInfoDO orderInfoDO = orderInfoDAO.getByOrderId(request.getOrderId());
        ParamCheckUtil.checkObjectNonNull(orderInfoDO, OrderErrorCodeEnum.ORDER_NOT_FOUND);

        //3、校验订单是否可以发起缺品
        if (!OrderStatusEnum.canLack().contains(orderInfoDO.getOrderStatus())){
            throw new OrderBizException(OrderErrorCodeEnum.ORDER_NOT_ALLOW_TO_LACK);
        }

        //4、查询订单的item
        List<OrderItemDO> orderItemDOList = orderItemDAO.listByOrderId(orderInfoDO.getOrderId());
        //5、校验具体的缺品项
        List<LackItemDTO> lackItems = new ArrayList<>();
        for (LackItemRequest lackItemRequest : request.getLackItems()) {
            lackItems.add(checkLackItem(orderInfoDO, orderItemDOList,lackItemRequest));
        }

        //6、构造返参
        return new CheckLackDTO(orderInfoDO, lackItems);
    }

    /**
     * 校验缺品项
     *
     * @return
     */
    private LackItemDTO checkLackItem(OrderInfoDO order, List<OrderItemDO> orderItems, LackItemRequest request) {
        String skuCode=request.getSkuCode();
        Integer lackNum = request.getLackNum();
        //1、参数校验
        ParamCheckUtil.checkStringNonEmpty(skuCode, OrderErrorCodeEnum.SKU_CODE_IS_NULL);
        ParamCheckUtil.checkIntMin(lackNum, 1, OrderErrorCodeEnum.LACK_NUM_IS_LT_0);

        //2、查询商品信息
        ProductSkuQuery query=new ProductSkuQuery();
        query.setSkuCode(skuCode);
        query.setSellerId(order.getSellerId());
        JsonResult<ProductSkuDTO> skuJsonResult = productApi.getProductSku(query);
        if (!skuJsonResult.getSuccess()){
            throw new OrderBizException(skuJsonResult.getErrorCode(), skuJsonResult.getErrorMessage());
        }

        ProductSkuDTO productSkuDTO=skuJsonResult.getData();
        ParamCheckUtil.checkObjectNonNull(productSkuDTO, OrderErrorCodeEnum.PRODUCT_SKU_CODE_ERROR, skuCode);
        //3、找到item中对应的缺品sku item
        OrderItemDO orderItemDO = orderItems.stream().filter(item -> item.getSkuCode().equals(skuCode))
                .findFirst().orElse(null);
        ParamCheckUtil.checkObjectNonNull(orderItemDO, OrderErrorCodeEnum.LACK_ITEM_NOT_IN_ORDER, skuCode);

        //4、缺品商品数量不能>=下单商品数量
        if (orderItemDO.getSaleQuantity() <= request.getLackNum()) {
            throw new OrderBizException(OrderErrorCodeEnum.LACK_NUM_IS_GE_SKU_ORDER_ITEM_SIZE);
        }

        //5、构造返参
        return new LackItemDTO(orderItemDO, lackNum, productSkuDTO);

    }

        @Override
    public LackDTO executeLackRequest(LackRequest request, CheckLackDTO checkLackDTO) {
        return null;
    }
}
